package net.oschina.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import net.oschina.app.Myapp;
import net.oschina.app.R;
import net.oschina.app.fragment.MeFragment;
import net.oschina.app.hjpbean.LoginBean;
import net.oschina.app.hjpbean.NoticeBean;
import net.oschina.app.hjpbean.UserBean;
import net.oschina.app.util.LogUtils;
import net.oschina.app.util.ZsfUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static net.oschina.app.Myapp.userBean;

/**
 * Created by admin on 2017/7/14.
 */

public class LogOnActivity extends AppCompatActivity {
    @BindView(R.id.user_name)
    EditText userName;
    @BindView(R.id.user_psw)
    EditText userPsw;
    @BindView(R.id.logon)
    Button logon;
    @BindView(R.id.share_qq)
    ImageView shareQq;
    @BindView(R.id.share_weichat)
    ImageView shareWeichat;
    @BindView(R.id.share_sinaweibo)
    ImageView shareSinaweibo;
    private int RESULTCODE = 102;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initVeiw();
        initEvent();
    }

    private void initEvent() {


    }

    private void initVeiw() {
        setContentView(R.layout.logon_layout);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.logon, R.id.share_qq, R.id.share_weichat, R.id.share_sinaweibo})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.logon:
                Toast.makeText(this, "登录中", Toast.LENGTH_SHORT).show();
                String name = userName.getText().toString().trim();
                String psw = userPsw.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(psw)) {
                    Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                //http://www.oschina.net/action/api/login_validate
                Call<LoginBean> login = Myapp.shopService.getLogin(1, name, psw);
                login.enqueue(new Callback<LoginBean>() {
                    @Override
                    public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                         Myapp.userBean = response.body().getUser();
                        ZsfUtils.saveObjcet(Myapp.getContext(),"userBean",userBean);
                        LoginBean.RusultBean result = response.body().getResult();
                        String errorMessage = result.getErrorMessage();
                        if (errorMessage.equals("登录成功")) {
                            Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show();
                            Myapp.isLogIn = true;
                            Myapp.sp.edit().putBoolean(Myapp.STATE_LOGIN, true).commit();
                            Intent intent = new Intent(LogOnActivity.this,MainActivity.class);
                            intent.putExtra("userBean",userBean);
                            setResult(RESULTCODE,intent);
                            finish();
                        } else {
                            Toast.makeText(getApplicationContext(), errorMessage.toString(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<LoginBean> call, Throwable t) {
                        Toast.makeText(getApplicationContext(), "联网失败", Toast.LENGTH_SHORT).show();
                    }
                });

                break;
            case R.id.share_qq:
                Toast.makeText(this, "来自QQ的登录", Toast.LENGTH_SHORT).show();
                break;
            case R.id.share_weichat:
                Toast.makeText(this, "来自微信的登录", Toast.LENGTH_SHORT).show();
                break;
            case R.id.share_sinaweibo:
                Toast.makeText(this, "来自新浪微博的登录", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
